package cn.work.control;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class logoutServlet extends HttpServlet {

    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        // 获取当前会话
        HttpSession session = request.getSession(false);

        if (session!= null) {
            // 清除会话中的用户相关信息（这里假设存储的用户信息的属性名为"user"）
            session.removeAttribute("user");

            // 使整个会话失效，这样用户的所有会话相关数据都会被清除
            session.invalidate();
        }

        // 重定向到登录页面（这里假设登录页面为Login.jsp，你可以根据实际情况修改）
        response.sendRedirect("login.jsp");
    }

    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}